<template>
  <div class="com-container">
    <div class="com-chart" ref="hot_ref"></div>
    <span class="iconfont arr-left" @click="toleft" :style="comStyle">&#xe6ef;</span>
    <span class="iconfont arr-right" @click="toright" :style="comStyle">&#xe6ed;</span>
    <span class="cat-name" :style="comStyle">{{catName }}</span>
  </div>
</template>

<script>
import { getHotData } from '@/API/api.js'
export default {
  name: 'Hot',
  data() {
    return {
      // echarts 初始化对象
      myChart: null,
      // 数据
      allData: null,
      currentIndex: 0,
      titleFontSize: 0
    }
  },
  mounted() {
    this.initChart()
    this.getData()
    window.addEventListener('resize', this.screenAdater)
    this.screenAdater()
  },
  destroyed() {
    window.removeEventListener('resize', this.screenAdater)
  },
  computed: {
    catName() {
      if (!this.allData) return ''
      return this.allData[this.currentIndex].name
    },
    comStyle() {
      return {
        fontSize: this.titleFontSize + 'px'
      }
    }
  },
  methods: {
    // 初始化 echarts 对象
    initChart() {
      this.myChart = this.$echarts.init(this.$refs.hot_ref, 'chalk')
      const initOption = {
        title: {
          text: '▎热销商品占比',
          top: 20,
          left: 20
        },
        series: [
          {
            type: 'pie',
            label: {
              show: false
            },
            emphasis: {
              label: {
                show: true
              },
              labelLine: {
                show: false
              }
            }
          }
        ]
      }
      this.myChart.setOption(initOption)
    },
    // 获取数据
    async getData() {
      const { data: res } = await getHotData()
      console.log(res)
      this.allData = res
      this.updateChart()
    },
    // 处理数据，更新图表
    updateChart() {
      const legendData = this.allData[this.currentIndex].children.map(item => {
        return item.name
      })
      const seriesData = this.allData[this.currentIndex].children.map(item => {
        return {
          name: item.name,
          value: item.value,
          children: item.children
        }
      })
      // const total
      const dataOption = {
        tooltip: {
          show: true,
          formatter: (arg) => {
            const thirdCategory = arg.data.children
            let total = 0
            thirdCategory.forEach((item) => {
              total += item.value
            })
            let str = ''
            arg.data.children.forEach(item => {
              str += `${item.name}:${parseInt(item.value / total * 100) + '%'}<br/>`
            })
            return str
          }
        },
        legend: {
          data: legendData
        },
        series: [
          {
            data: seriesData
          }
        ]
      }
      this.myChart.setOption(dataOption)
    },
    // 分辨率适配
    screenAdater() {
      this.titleFontSize = this.$refs.hot_ref.offsetWidth / 100 * 3.6
      const adaterOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize
          }
        },
        legend: {
          top: '10%',
          icon: 'circle',
          itemWidth: this.titleFontSize / 2,
          itemHeight: this.titleFontSize / 2,
          itemGap: this.titleFontSize / 2
        },
        series: {
          radius: this.titleFontSize * 4.5,
          center: ['50%', '54%']
        }
      }
      this.myChart.setOption(adaterOption)
      this.myChart.resize()
    },
    toleft() {
      this.currentIndex--
      if (this.currentIndex < 0) {
        this.currentIndex = this.allData.length - 1
      }
      this.updateChart()
    },
    toright() {
      this.currentIndex++
      if (this.currentIndex > this.allData.length - 1) {
        this.currentIndex = 0
      }
      this.updateChart()
    }
  }
}
</script>

<style lang="less" scoped>
.arr-left, .arr-right {
  position: absolute;
  top: 50%;
  color: #fff;
  transform: translateY(-50%);
  cursor: pointer;
}
.arr-left {
  left: 10%;
}
.arr-right {
  right: 10%;
}

.cat-name {
  position: absolute;
  bottom: 30px;
  right: 20%;
  color: #fff;
}
</style>
